package Clothing.serviceImpl.returns;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import Clothing.bean.request.lease.EditLeaseByIdRequest;
import Clothing.bean.request.lease.GetAllLeaseRequest;
import Clothing.bean.request.returns.AddReturnRequest;
import Clothing.bean.respose.returns.GetAllReturnResponse;
import Clothing.bean.respose.returns.GetAllReturnResponse.ReturnInfo;
import Clothing.dao.ClothingDao;
import Clothing.dao.LeaseDao;
import Clothing.dao.ReturnDao;
import Clothing.entity.ClothingEntity;
import Clothing.entity.LeaseEntity;
import Clothing.entity.ReturnAllEntity;
import Clothing.entity.ReturnEntity;
import Clothing.service.returns.ReturnService;
import Clothing.util.Utils;

@Service("returnService")
public class ReturnServiceImpl implements ReturnService {

    @Autowired
    private ClothingDao clothingDao;

    @Autowired
    private LeaseDao    leaseDao;
    
    @Autowired
    private ReturnDao returnClothingDao;

    public int getAllReturn(GetAllLeaseRequest request, GetAllReturnResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<ReturnAllEntity>  returnClothingEntities = new ArrayList<ReturnAllEntity>();
        map.put("clothingName", request.getClothingName());
        map.put("clothingType", request.getClothingType());
        map.put("returnTime", request.getReturnTime());
        map.put("leaseTime", request.getLeaseTime());
        returnClothingEntities = returnClothingDao.getAll(map);
        
        List<ReturnInfo> leaseInfoList = new ArrayList<ReturnInfo>();
        for (ReturnAllEntity returnEntity : returnClothingEntities) {
            ReturnInfo leaseInfo = new ReturnInfo();
            BeanUtils.copyProperties(returnEntity, leaseInfo);
            leaseInfo.setLeaseTime(Utils.dateToStr(returnEntity.getLeaseTime()));
            leaseInfo.setReturnTime(Utils.dateToStr(returnEntity.getReturnTime()));
            leaseInfoList.add(leaseInfo);
        }
        response.setReturnInfoList(leaseInfoList);
        return 0;
    }

    public int editReturnById(EditLeaseByIdRequest request) {
        return 0;
    }

    public int addReturn(AddReturnRequest request) {
        Map<String, Object> clothingMap = new HashMap<String, Object>();
        String leaseCount = request.getLeaseCount();
        String leaseId = request.getLeaseId();
        String returnTime = request.getReturnTime();
        if (StringUtils.isBlank(leaseCount) || StringUtils.isBlank(leaseId)
            || StringUtils.isBlank(returnTime)) {
            return 1;
        }
        LeaseEntity leaseEntity = null;
        leaseEntity = leaseDao.selectByPrimaryKey(Integer.parseInt(leaseId));
        if (leaseEntity == null) {
            return 1;
        }
        ClothingEntity clothingEntity = clothingDao.selectByPrimaryKey(leaseEntity.getClothingId());
        int dbCount = leaseEntity.getLeaseCount();
        int count = Integer.parseInt(request.getLeaseCount());
        int diffCount = dbCount - count;

        clothingMap.put("stock", count + clothingEntity.getStock());
        LeaseEntity leasePartEntity1 = new LeaseEntity();
        ReturnEntity returnEntity = new ReturnEntity();
        if (diffCount > 0) {//未完全归还
            BeanUtils.copyProperties(leaseEntity, leasePartEntity1);
            leasePartEntity1.setLeaseCount(diffCount);
            leasePartEntity1.setIsFinish("0");
            leaseDao.updateByPrimaryKey(leasePartEntity1);
            
            //修改库存
            clothingMap.put("clothingId", leasePartEntity1.getClothingId());
            clothingDao.updateByKey(clothingMap);

            BeanUtils.copyProperties(leasePartEntity1, returnEntity);
            returnEntity.setStatus("2");
            returnEntity.setReturnTime(Utils.convertToDate(request.getReturnTime()));
            returnEntity.setReturnCount(count);
            returnClothingDao.insertSelective(returnEntity);
        } else if (diffCount == 0) {
            BeanUtils.copyProperties(leaseEntity, leasePartEntity1);
            leasePartEntity1.setLeaseCount(diffCount);
            leasePartEntity1.setIsFinish("1");
            leaseDao.updateByPrimaryKey(leasePartEntity1);

            //修改库存
            clothingMap.put("clothingId", leasePartEntity1.getClothingId());
            clothingDao.updateByKey(clothingMap);
            
            BeanUtils.copyProperties(leasePartEntity1, returnEntity);
            returnEntity.setStatus("2");
            returnEntity.setReturnTime(Utils.convertToDate(request.getReturnTime()));
            returnEntity.setReturnCount(count);
            returnClothingDao.insertSelective(returnEntity);

            List<ReturnEntity> returnEntities = new ArrayList<ReturnEntity>();
            returnEntities = returnClothingDao.selectByLeaseID(leaseEntity.getLeaseId());
            for (ReturnEntity returnEntity2 : returnEntities) {
                returnEntity2.setIsFinish("1");//设置该leaseId的全部归还状态为已完成
                returnClothingDao.updateByPrimaryKeySelective(returnEntity2);
            }
        } else {
            return 1;
        }

        return 0;
    }

}
